<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/18
 * Time: 10:41
 */
namespace app\admin\controller;
use think\Request;

class Log extends Common
{
    /**
     * 系统消息页面
     * @return mixed
     */
    public function sysLogUI(){
        return $this->fetch('sysLog');
    }

    /**
     * 微信模板消息页面
     * @return mixed
     */
    public function weLogUI(){
        return $this->fetch('weLog');
    }

    /**
     * 系统日志列表
     * @param Request $request
     * @return array
     */
    public function sysQuery(Request $request)
    {
        $limit = $request->param('limit');
        $input = $request->param();
        $rows = db('log')->alias('l')
                ->order('id','desc')
                ->where(function ($query)use($input){
                    if(!empty($input['key']))
                        $query->where('a.name|a.username|l.ip',$input['key']);
                })
                ->join('admin a','a.admin_id=l.uid','left')
                ->field(['l.*','a.username','a.name'])
                ->paginate($limit)->toArray();
        return ['code'=>0,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 删除系统记录
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function sdel(Request $request){
        $ids = $request->param('ids');
        $arr = explode(',',$ids);
        $row = db('log')->where('id','in',$arr)->delete();
        if($row)
            return ['code'=>1,'msg'=>'删除成功！'];
        return ['code'=>0,'msg'=>'删除失败！'];
    }

    /**
     * 微信日志列表
     * @param Request $request
     * @return array
     */
    public function weQuery(Request $request)
    {
        $limit = $request->param('limit');
        $input = $request->param();
        $rows = db('wx_log')->alias('l')
            ->order('id','desc')
            ->where(function ($query)use($input){
                if(!empty($input['key']))
                    $query->where('a.name|a.username',$input['key']);
            })
            ->join('admin a','a.openid=l.openid','left')
            ->field(['l.*','a.username','a.name'])
            ->paginate($limit)->toArray();
        return ['code'=>0,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 删除微信模板记录
     * @param Request $request
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function wdel(Request $request){
        $ids = $request->param('ids');
        $arr = explode(',',$ids);
        $row = db('wx_log')->where('id','in',$arr)->delete();
        if($row)
            return ['code'=>1,'msg'=>'删除成功！'];
        return ['code'=>0,'msg'=>'删除失败！'];
    }

}